package com.yunmang.activity.qianshou;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.yunmang.R;
import com.yunmang.activity.BaseActivity;
import com.yunmang.adapter.MyPagerAdapter;
import com.yunmang.adapter.NosendAdapter;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.ParameterEntity.SignBean;
import com.yunmang.fragment.BaseFragment;
import com.yunmang.fragment.qianshou.NoSendFragment;
import com.yunmang.fragment.qianshou.SendFailFragment;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/10.
 */
public class QianshouActivity extends BaseActivity {
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.tabLayout)
    TabLayout tabLayout;
    @InjectView(R.id.viewPager)
    ViewPager viewPager;
    @InjectView(R.id.et_hdbh_search)
    EditText etHdbhSearch;
    @InjectView(R.id.image_scan)
    ImageView imageScan;
    @InjectView(R.id.ll_search)
    LinearLayout llSearch;
    @InjectView(R.id.search)
    Button search;
    @InjectView(R.id.ll_container)
    LinearLayout llContainer;
    private List<BaseFragment> fragmentList;
    private Subscriber subscriber;
    private List<SignBean.ModelBean.ItemsBean> itemList;
    @Override
    public int getLayoutId() {
        return R.layout.activity_qianshou;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        initToolbar();
    }


    private void initToolbar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("派送签收");
        mToolbar.setNavigationIcon(R.mipmap.ic_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new NoSendFragment());
        fragmentList.add(new SendFailFragment());
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), fragmentList));
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    @OnClick({R.id.image_scan, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_scan:
                CommonUtils.onScanBarcode(this);
                break;
            case R.id.search:
                String hdbh=CommonUtils.getTextViewValue(etHdbhSearch);
                if(TextUtils.isEmpty(hdbh)){
                    showToast("请输入单号");
                    return;
                }
                downSignInfo(hdbh);
                break;
        }
    }
    /**
     * 下载信息
     * @param hdbh
     */
    private void downSignInfo(String hdbh) {
        showWaitingDialog("数据加载中……");
        subscriber = new Subscriber<SignBean>() {
            @Override
            public void onCompleted() {
                hideWaitingDialog();
            }

            @Override
            public void onError(Throwable e) {
                hideWaitingDialog();
            }

            @Override
            public void onNext(SignBean signBean) {
                hideWaitingDialog();
                if (signBean.getCode() == 200) {
                    itemList = signBean.getModel().getItems();
                    if(itemList.size()==0){
                        showToast("暂无数据");
                        return;
                    }
                    //在这里需要通知fragment去刷新
                    EventBus.getDefault().post(itemList.get(0).getId()+"");
                } else {
                    showToast("下载失败");
                }
            }
        };
        HttpUtils.getInstance().downloadSignInfo(subscriber, "",hdbh, AppConstant.orgInfo.getId() + "");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "扫码取消！", Toast.LENGTH_LONG).show();
            } else {
                etHdbhSearch.setText(result.getContents());
//                Toast.makeText(this, "扫描成功，条码值: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
